var shopOrderId = GetQueryString('commentId');
$(function () {
    doOrder();
})

function doOrder() {
    $.ajax({
        url: '/purchaseOrderC/orderComment.json',
        dataType: 'json',
        data: {
            shopOrderId: shopOrderId,
            token: getItem('token'),
            companyCode:getItem('companyCode'),
        },
        type: 'post',
        success: function (data) {
            if (data.code == 1) {
                var goodsData = data.datas.goodsList;
                for (var i = 0; i < goodsData.length; i++) {
                    $('<div class="goods_comment" goodsId="' + goodsData[i].goodsId + '" orderId="'+goodsData[i].id+'">\n' +
                        '<div class="goods_title mui-clearfix">\n' +
                        '<div class="small_img"><img src="' + goodsData[i].goodsImg.split(',')[0] + '" alt=""></div>\n' +
                        '<div class="goods_comment_fr">\n' +
                        '<div class="grade_text">评分</div>\n' +
                        '<div class="goods_grade">\n' +
                        '<img src="../phoneImg/doComment_star.jpg" alt="1分" goodsStar="true" goodsText="不满意">\n' +
                        '<img src="../phoneImg/doComment_star.jpg" alt="2分" goodsStar="true" goodsText="不满意">\n' +
                        '<img src="../phoneImg/doComment_star.jpg" alt="3分" goodsStar="true" goodsText="一般">\n' +
                        '<img src="../phoneImg/doComment_star.jpg" alt="4分" goodsStar="true" goodsText="好">\n' +
                        '<img src="../phoneImg/doComment_star.jpg" alt="5分" goodsStar="true" goodsText="非常满意">\n' +
                        '</div><div class="grade_dec" goodsText="5" style="display: none">非常好</div></div></div><div class="goods_detail">\n' +
                        '<textarea placeholder="评价这个商品..."></textarea>\n' +
                        '</div><div class="goods_img mui-clearfix" goodsId="' + goodsData[i].goodsId + '" goodsImg="">\n' +
                        '<img src="../phoneImg/add_photo.jpg" alt="添加图片">\n' +
                        '<img src="../phoneImg/add_photo.jpg" alt="添加图片" style="display: none;">\n' +
                        '<img src="../phoneImg/add_photo.jpg" alt="添加图片" style="display: none;">\n' +
                        '<img src="../phoneImg/add_photo.jpg" alt="添加图片" style="display: none;">\n' +
                        '<img src="../phoneImg/add_photo.jpg" alt="添加图片" style="display: none;">\n' +
                        '<label class="file">' +
                        '<input class="file_input" id="' + goodsData[i].goodsId + '" name="file" type="file" accept="image/*;capture=camera;">' +
                        '</label>' +
                        '</div></div><div class="line"></div>').appendTo($('.goods_comment_box'));
                }
            }
        }
    })

    //商品星级评分
    $('body').on('click', '.goods_grade img', function () {
        if ($(this).attr('goodsStar') == 'true') {
            $(this).nextAll().attr({'goodsStar': 'false', 'src': '../phoneImg/doComment_star_h.png'})
        } else {
            $(this).attr({'goodsStar': 'true', 'src': '../phoneImg/doComment_star.jpg'})
            $(this).prevAll().attr({'goodsStar': 'true', 'src': '../phoneImg/doComment_star.jpg'})
        }
    })

//添加评价
    $('#save').click(function () {
        var datas = [];
        $('.goods_comment').each(function () {
            var goodsComment = {};
            goodsComment.goodId = $(this).attr('goodsid');
            var comments = $(this).children('.goods_detail').children().val();
            if (comments == "") {
                comments = "好评！"
            }
            goodsComment.content = comments;
            var goodsGrade = $(this).find('.grade_dec').attr('goodsText');
            goodsComment.gradedValue = goodsGrade;
            var orderId = $(this).attr('orderId');
            goodsComment.orderId = orderId;
            var goodsImg = $(this).find('.goods_img').attr('goodsImg');
            goodsComment.imgUrl = goodsImg;
            datas.push(goodsComment);
        })
        $.ajax({
            url: '/commentsC/addComments.json',
            dataType: 'json',
            data: {
                token: getItem('token'),
                shopOrderId: shopOrderId,
                datas: JSON.stringify(datas),
                companyCode:getItem('companyCode'),
            },
            type: 'post',
            success: function (data) {
                if (data.code == 1) {
                    mui.alert('评价成功', function () {
                        window.location.href = "order.html"
                    })
                }
                if (data.code == -1) {
                    window.location.href = "login.html"
                }
                if (data.code == 0) {
                    mui.toast(data.msg);
                    return;
                }
            }
        })
    })

//商品上传图片
    if(ifApp()==0||ifApp()==1){
        $('body').on('change', '.file_input', function () {
            var tThis = $(this);
                var loadId = tThis.attr('id');
                tThis.trigger("click");
                changeImg2(loadId);
        })
    }else{
        //android
        $('body').on('click', '.file_input', function () {
            var tThis = $(this);
            var loadId = tThis.attr('id');
            window.ddw.gopingjia(loadId);
        })
    }

    function changeImg2(a) {
        var imgs2 = "";
        var IMG2 = 0;
        if (IMG2 > 4) {
            mui.alert('图片已上传完毕！');
            return;
        } else {
            selectOver2(a);
        }

        function selectOver2(a) {
            IMG2++;
            $.ajaxFileUpload({
                url: '/uploadImage.do',
                type: 'POST',
                fileElementId: a,
                dataType: 'JSON',
                success: function (data) {
                    var results2 = data.split('|');
                    var flag2 = results2[0].split('>');
                    if (flag2[1] != 'success') {
                        mui.alert('上传失败');
                        IMG2--;
                        return;
                    }

                    var img2 = results2[1].substring(results2[1].length - 6, 0);
                    IMG2++;

                    var imgBox = $('#' + a + '').parent().parent().find('img');
                    if ($(imgBox[0]).attr('src') == '../phoneImg/add_photo.jpg') {
                        $(imgBox[0]).attr('src', img2);
                        imgs2 = imgs2 + $(imgBox[0]).attr("src");
                        $(imgBox[1]).attr("src", "../phoneImg/add_photo.jpg");
                        $(imgBox[1]).css('display', 'block');
                    } else if ($(imgBox[1]).attr('src') == '../phoneImg/add_photo.jpg') {
                        $(imgBox[1]).attr('src', img2);
                        imgs2 = imgs2 + "," + $(imgBox[1]).attr("src");
                        $(imgBox[2]).attr("src", "../phoneImg/add_photo.jpg");
                        $(imgBox[2]).css('display', 'block');
                    } else if ($(imgBox[2]).attr('src') == '../phoneImg/add_photo.jpg') {
                        $(imgBox[2]).attr('src', img2);
                        imgs2 = imgs2 + "," + $(imgBox[2]).attr("src");
                        $(imgBox[3]).attr("src", "../phoneImg/add_photo.jpg");
                        $(imgBox[3]).css('display', 'block');
                    } else if ($(imgBox[3]).attr('src') == '../phoneImg/add_photo.jpg') {
                        $(imgBox[3]).attr('src', img2);
                        imgs2 = imgs2 + "," + $(imgBox[4]).attr("src");
                        $(imgBox[4]).attr("src", "../phoneImg/add_photo.jpg");
                        $(imgBox[4]).css('display', 'block');
                    } else if ($(imgBox[4]).attr("src") == "" || $(imgBox[4]).attr('src') == '../phoneImg/add_photo.jpg') {
                        $(imgBox[4]).attr('src', img2);
                        imgs2 = imgs2 + "," + $(imgBox[4]).attr("src");
                    }
                    imgs2 = '';
                    $('.goods_img img').each(function () {
                        if($(this).attr('src')!='../phoneImg/add_photo.jpg'){
                            imgs2+=$(this).attr('src')+',';
                        }
                    })
                    $('#' + a + '').parent().parent().attr('goodsImg', imgs2);
                    var fileHeight = $('.goods_img').height()-10;
                    $('.file_input').css('height',fileHeight+'px');

                },
                error: function (data) {
                    IMG2--;
                    mui.alert('上传失败');

                }
            })
        }
    }
}

function chaImage(id,url) {
    var imgUrl = url.split(',');
    var imgs2 = '';
    var oDiv = $('#' + id + '').parent().parent().find('img');
    var imgUrlLenth = imgUrl.length-1;
        for(var i=0;i<imgUrlLenth;i++){
            //alert($(oDiv[i]));
            $(oDiv[i]).attr('src',imgUrl[i]);
            $(oDiv[i]).show();
            $(oDiv[parseInt(i+1)]).show();
        }
        $(oDiv).each(function () {
            $(oDiv[imgUrlLenth]).show();
            return;
        })
    var fileHeight = $('.goods_img').height()-10;
    $('.file_input').css('height',fileHeight+'px');
    var goodsImg = $('#' + id + '').parent().parent().attr('goodsImg');
    if(goodsImg==""){}else{
        goodsImg = goodsImg+url
    }
    $('#' + id + '').parent().parent().attr('goodsImg', url);
}